/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service.beans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.struts.service.OperatorsConfig;
import opiam.admin.faare.struts.service.StrutsService;
import opiam.admin.faare.struts.service.beans.JBOperator;
import opiam.admin.faare.struts.service.beans.JBOperators;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;


/**
 * Test of the JBOperators class.
 */
public class JBOperatorsTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBOperatorsTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Operators configuration file name. */
    public static final String OPERATORS_XML_FILE = "/config/service/struts/operators_conf.xml";

    /** Instance of JBOperators. */
    private JBOperators jbOperatorsLoaded = new JBOperators();

    /**
     * Creates a new JBOperators object.
     *
     * @param arg0  Parameter.
     */
    public JBOperatorsTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
            jbOperatorsLoaded = OperatorsConfig.getInstance().readConfig(StrutsService.class.getResourceAsStream(
                        OPERATORS_XML_FILE));
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBOperatorsTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();
        suite.addTest(new JBOperatorsTest("testBegin"));

        suite.addTest(new JBOperatorsTest("testGetResourcename"));
        suite.addTest(new JBOperatorsTest("testFindOperatorByName"));

        suite.addTest(new JBOperatorsTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBOperatorsTest");

        _test.displayHeaderClass("JBOperatorsTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBOperatorsTest");
    }

    /**
     * Test the methods: getResourcename.
     *
     */
    public void testGetResourcename()
    {
        _test.displayHeaderMethod("testGetResourcename");

        try
        {
            String expected = "config.appli_messages";

            String result = jbOperatorsLoaded.getResourcename();

            if (expected.equals(result))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGetResourcename");
    }

    /**
     * Test the method: findRequestByName.
     *
     */
    public void testFindOperatorByName()
    {
        _test.displayHeaderMethod("testFindOperatorByName");

        try
        {
            String expectedjbOperator = "search_sn";
            JBOperator resultjbOperator = jbOperatorsLoaded.findOperatorByName(
                    "search_sn");

            if (expectedjbOperator.equals(resultjbOperator.getName()))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exeption : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindOperatorByName");
    }
}
